var $pagePath, $pageTitle, $setMealBox, $identifyInput, $identifyResult, $contentWrapper, $queryContent, $setSecondNav, $queryResult, $combinationContent, $developing, $accountsNumber, $userManage, $changePassword;
var stepMap;
// 设置一级导航栏
function setMainNav(navData) {
    var $mainNavUl = $("#mainNavUl");
    var lisHtml = '';
    var classValue = 'active';
    for (var i = 0, l = navData.length; i < l; i++) {
        if (i == 0) {
            setSecondNav(navData[i].child);
        } else {
            classValue = '';
        }
        lisHtml += '<li class="' + classValue + '" data-value="' + navData[i].value + '"><a href="javascript:;">' + navData[i].name + '</a></li>';
    }

    $mainNavUl.html(lisHtml);
}

// 设置二级导航
function setSecondNav(sencondData, theNavLiVal) {

    var slisHtml = '';
    for (var i = 0, l = sencondData.length; i < l; i++) {
        var active = "";
        if (!theNavLiVal && i == 0) {
            active = "active";
        } else if (theNavLiVal == sencondData[i].value) {
            active = "active";
        }
        slisHtml += `
<li class="${active}">
    <a href="javascript:;" data-value="${sencondData[i].value}">
        <i class="${sencondData[i].iclass}"></i>
        <span>${sencondData[i].name}</span>
    </a>
</li>
`;
    }
    // console.log(slisHtml);
    $("#setSecondNav").html(slisHtml);
}


// 设置head1中的时间
function setHeadDate() {
    var dayMap = {
        "0": "星期天",
        "1": "星期一",
        "2": "星期二",
        "3": "星期三",
        "4": "星期四",
        "5": "星期五",
        "6": "星期六",
    };
    var nowdate = new Date();
    var thedate = nowdate.getDate() < 10 ? "0" + nowdate.getDate() : nowdate.getDate();
    var themonth = nowdate.getMonth() < 9 ? "0" + (nowdate.getMonth() + 1) : (nowdate.getMonth() + 1);
    var therq = nowdate.getFullYear() + "-" + themonth + "-" + thedate;
    var dayIndex = nowdate.getDay();
    $("#headDate").text(therq);
    $("#headDay").text(dayMap[dayIndex]);
}

// 设置内容右边的页面路径
function setPagerPath(pathArray) {
    var pathLiHtml = '<li><i class="icon-bar-chart"></i></li>';
    for (var i = 0, l = pathArray.length; i < l; i++) {
        pathLiHtml += `<li class="separator"><i class="icon-angle-right"></i></li><li>${pathArray[i]}</li>`;
    }

    $pagePath.html(pathLiHtml);
}


// 显示步骤
function showStep(step) {
    var liIndex = stepMap[step].index;
    $contentWrapper.find(".step-fluid").hide();
    stepMap[step].$dom.show();
    setPagerPath(stepMap[step].PagerPath);

    $setSecondNav.find(".active").removeClass("active");
    $("#setSecondNav li:eq(" + liIndex + ")").addClass("active");

}

// 查询没有登录
function needLoading(ndata) {
    if (ndata && ndata.code == "login_need") {
        location.href = "./index.html";
    }
}

// 设置内容区页面标题
function setPageTitle(pageTitle, iclass) {
    if (iclass) {
        $pageTitle.find("i").attr("class", iclass);
    }
    $pageTitle.find("span").text(pageTitle);
}

// 设置步骤的对应关系
function setStepMap() {
    stepMap = {
        "verify0": {
            "index": 0,
            "$dom": $setMealBox,
            "PagerPath": ["查询", "选择组合"]

        },
        "verify1": {
            "index": 1,
            "$dom": $identifyInput,
            "PagerPath": ["查询", "选择组合", "信息采集"]

        },
        "verify2": {
            "index": 2,
            "$dom": $identifyResult,
            "PagerPath": ["查询", "选择组合", "信息采集", "查询结果"]

        },
        "query0": {
            "index": 0,
            "$dom": $queryContent,
            "PagerPath": ["查询", "查询当天记录"]

        },
        "query1": {
            "index": 1,
            "$dom": $queryContent,
            "PagerPath": ["查询", "条件查询记录"]
        },
        "query2": {
            "index": 1,
            "$dom": $queryResult,
            "PagerPath": ["查询", "条件查询记录", "查询详情"]

        },
        "combination": {
            "index": 0,
            "$dom": $combinationContent,
            "PagerPath": ["查询", "现有组合"]
        },
        "combination1": {
            "index": 1,
            "$dom": $developing,
            "PagerPath": ["查询", "组合设定"]
        },
        "accounts0": {
            "index": 0,
            "$dom": $accountsNumber,
            "PagerPath": ["帐务管理", "计次管理"]
        },
        "userManage": {
            "index": 0,
            "$dom": $userManage,
            "PagerPath": ["用户管理", "机构管理"]
        },
        "changePassword": {
            "index": 0,
            "$dom": $changePassword,
            "PagerPath": ["修改密码"]
        }

    };
}

// 设置查询记录详情
function setQuery2Index(qindex) {
    stepMap["query2"].index = qindex;
}

// 转换时间
function getDateFormate(formart, newDate) {
    // format: "yyyy-mm-dd hh:ii:ss"  | "yyyy-mm-dd"
    var dateSty = arguments[0].split(' '),
        separatorFirst = '',
        separatorSecond = '',
        realDate = '';
    var yyyy = newDate.getFullYear(),
        mm = newDate.getMonth() + 1,
        dd = newDate.getDate(),
        hh = newDate.getHours(),
        ii = newDate.getMinutes(),
        ss = newDate.getSeconds();
    mm = mm < 10 ? '0' + mm : mm;
    dd = dd < 10 ? '0' + dd : dd;
    hh = hh < 10 ? '0' + hh : hh;
    ii = ii < 10 ? '0' + ii : ii;
    ss = ss < 10 ? '0' + ss : ss;

    if (dateSty.length > 1) {
        //  console.log(dateSty);
        separatorFirst = dateSty[0].charAt(4);
        separatorSecond = dateSty[1].charAt(2);
        realDate = yyyy + separatorFirst + mm + separatorFirst + dd + ' ' + hh + separatorSecond + ii + separatorSecond + ss;
    } else {
        separatorFirst = dateSty[0].charAt(4);
        realDate = yyyy + separatorFirst + mm + separatorFirst + dd;
    }

    return realDate;
}
// 初始化变量
(function initCommonValue() {
    $pagePath = $("#page-header_path");
    $pageTitle = $("#page-header_title");
    $setMealBox = $("#set-meal_box");
    $identifyInput = $("#identify-input");
    $identifyResult = $("#identify-result");
    $contentWrapper = $("#content-wrapper");
    $queryContent = $("#query-content");
    $setSecondNav = $("#setSecondNav");
    $queryResult = $("#query-result");
    $combinationContent = $("#combination-content");
    $developing = $("#developing");
    $accountsNumber = $("#accounts-number");
    $userManage = $("#user-manage");
    $changePassword = $("#change-password");
    setStepMap();
})();


module.exports = {
    setMainNav: setMainNav,
    setSecondNav: setSecondNav,
    setHeadDate: setHeadDate,
    setPagerPath: setPagerPath,
    setPageTitle: setPageTitle,
    showStep: showStep,
    getDateFormate: getDateFormate,
    needLoading: needLoading,
    setQuery2Index: setQuery2Index
}